import React, { useEffect, useState } from 'react';
import Home from '../../views/sandbox/home/Home'
import Userlist from '../../views/sandbox/user-manage/UserList'
import Rightlist from '../../views/sandbox/right-manage/RightList'
import Rolelist from '../../views/sandbox/right-manage/RoleList'
import Nopermission from '../../views/sandbox/nopermission/NoPermission'
import NewsAdd from '../../views/sandbox/news-manage/NewsAdd'
import NewsDraft from '../../views/sandbox/news-manage/NewsDraft'
import NewsCategory from '../../views/sandbox/news-manage/NewsCategory'
import Audit from '../../views/sandbox/audit-manage/Audit'
import AuditList from '../../views/sandbox/audit-manage/AuditList'
import Unpublished from '../../views/sandbox/pubish-manage/Unpublished'
import Published from '../../views/sandbox/pubish-manage/Published'
import Sunset from '../../views/sandbox/pubish-manage/Sunset'
import { HashRouter, Redirect, Route, Switch } from 'react-router-dom'
import axios from 'axios'
import { CheckCircleOutlined } from '@ant-design/icons';
import Newspreview from '../../views/sandbox/news-manage/NewsPreview';
import { Spin } from 'antd'
import {connect} from 'react-redux'
const LocalRouterMap = {
  "/home": Home,
  "/user-manage/list": Userlist,
  "/right-manage/role/list": Rolelist,
  "/right-manage/right/list": Rightlist,
  "/news-manage/add": NewsAdd,
  "/news-manage/draft": NewsDraft,
  "/news-manage/category": NewsCategory,
  "/news-manage/preview/:id": Newspreview,
  "/news-manage/update/:id": NewsAdd,
  "/audit-manage/audit": Audit,
  "/audit-manage/list": AuditList,
  "/publish-manage/unpublished": Unpublished,
  "/publish-manage/published": Published,
  "/publish-manage/sunset": Sunset

}
const Newsrouter = (props) => {
  const [BackRouteList,setBackRouteList] = useState([])
  useEffect(() => {
    Promise.all([
      axios.get('/rights'),
      axios.get('/children')
    ]).then(res => {
      setBackRouteList([...res[0].data,...res[1].data])
      
    })
  }, []);
   const {role:{rights}} = JSON.parse(localStorage.getItem('token'))
  // 判断用户开关是否关了
  const CheckRoute = (item) => {
    return LocalRouterMap[item.key] && (item.pagepermisson || item.routepermisson)
  }
  const checkUserPermission = (item) => {
    return rights.includes(item.key)
  }
  return (
    
       <Spin spinning={props.isLoding}>
        <Switch>
          {
            BackRouteList.map(item => {
              if(CheckRoute(item) && checkUserPermission(item)) {
              return  <Route path={item.key} key={item.key} component={LocalRouterMap[item.key]} exact/>
            }
              return null
            })
          }
          <Redirect from="/" to="/home" exact />
          {
            BackRouteList.length > 0 &&  <Route path="*" component={Nopermission}/>
          }
        
          </Switch>
        </Spin>
  )
}
// 映射出去
const mapStateToProps = ({ LodingReducer: { isLoding } }) => {
  return {
    isLoding
  }
}
export default connect(mapStateToProps)(Newsrouter)
